$(document).ready(function() {
    $.widget("custom.catcomplete", $.ui.autocomplete, {
        _renderMenu: function(ul, items) {
            var that = this,
                    currentCategory = "";
            $.each(items, function(index, item) {
                if (item.category != currentCategory) {
                    ul.append("<li class='ui-autocomplete-category'>" + item.category + "</li>");
                    currentCategory = item.category;
                }
                that._renderItemData(ul, item);
            });
        }
    });

    var data = [
        {label: "anders", category: ""},
        {label: "andreas", category: ""},
        {label: "antal", category: ""},
        {label: "annhhx10", category: "Products"},
        {label: "annk K12", category: "Products"},
        {label: "annttop C13", category: "Products"},
        {label: "anders andersson", category: "People"},
        {label: "andreas andersson", category: "People"},
        {label: "andreas johnson", category: "People"}
    ];
    
    $( "#search_textbox" ).catcomplete({
      delay: 100,
      source: "./API/search_autocomplete.php"
    });
    
    /*$("#search_textbox").autocomplete({
        source: "./API/search_autocomplete.php",
        minLength: 2
    });*/
    $(document).ready(function() {
        $("#checkin").datepicker("setDate", "0d");
        $("#checkout").datepicker("setDate", "+1d");
    });
    $("#checkin").datepicker("setDate", "0d");
    $("#checkout").datepicker("setDate", "+1d");
    var todaysDate = new Date();

    $("#checkin").datepicker({
        minDate: "0d",
        maxDate: "+499d",
        onSelect:
                function(selectedDate) {
                    if ($('#checkin').datepicker('getDate') != null) {
                        var currentDate = $('#checkin').datepicker('getDate');
                        currentDate.setDate(currentDate.getDate() + 1);
                        $('#checkout').datepicker('option', 'minDate', currentDate);
                        var maxDate = $('#checkin').datepicker('getDate');
                        maxDate.setDate(maxDate.getDate() + 27);
                        $('#checkout').datepicker('option', 'maxDate', maxDate);
                    }
                },
        onClose:
                function(selectedDate) {
                    var currentDate = $('#checkin').datepicker('getDate');
                    var item_length = jQuery('#checkin').val().length;
                    if (currentDate == null || item_length != 10) {
                        $('#checkin').datepicker('setDate', "0d");
                    }
                }
    });


    $("#checkout").datepicker({
        minDate: "+1d",
        maxDate: "+5",
        beforeShow:
                function(selectedDate) {
                    if ($('#checkin').datepicker('getDate') != null) {
                        var currentDate = $('#checkin').datepicker('getDate');
                        currentDate.setDate(currentDate.getDate() + 1);
                        $('#checkout').datepicker('option', 'minDate', currentDate);
                        var maxDate = $('#checkin').datepicker('getDate');
                        maxDate.setDate(maxDate.getDate() + 27);
                        $('#checkout').datepicker('option', 'maxDate', maxDate);
                    }
                },
        onClose:
                function(selectedDate) {
                    var check_in_date = $('#checkin').datepicker('getDate');
                    var check_out_date = $('#checkout').datepicker('getDate');
                    var maxDate = $("#checkout").datepicker("option", "maxDate");
                    var item_length = jQuery('#checkout').val().length;
                    if ((check_in_date > check_out_date) || (check_out_date > maxDate) || (item_length != 10)) {
                        $('#checkout').datepicker('setDate', check_in_date);
                    }
                }
    });
});


function search_validate() {
    if (document.getElementById('search_textbox').value.length <= 0) {
        document.getElementById('search_textbox').style.border = "2px solid #F00";
        document.getElementById('search_error').innerHTML = "Please enter a location."
    } else {
        document.getElementById("search_form").submit();
    }
}
;

function checkDestination() {
    if (document.getElementById('search_textbox').value.length <= 0) {
        document.getElementById('search_textbox').style.border = "2px inset #F00";
        document.getElementById('search_error').innerHTML = "Please enter a location.";
    } else {
        document.getElementById('search_textbox').style.border = "2px inset #000";
        document.getElementById('search_error').innerHTML = "";
    }
}
;
